import random


def hangman(word_list):
    """
    Hangman Game
    :type word_list: list
    :param word_list: the list contains all the words to guess
    :return: None
    """
    count = len(word_list)
    random_number = random.randint(0, count - 1)
    word = word_list[random_number]
    wrong_guesses = 0
    stages = ["",
              "________      ",
              "|      |      ",
              "|      0      ",
              "|     /|\     ",
              "|     / \     ",
              "|"]
    # init as word, if a char guessed, marked as $
    remaining_letters = list(word)
    letter_board = ["__"] * len(word)
    win = False

    # GAME START
    print('Welcome to Hangman')
    # LOOP - 6 WRONG TRIES
    while wrong_guesses < len(stages) - 1:
        print('\n')
        guess = input("Guess a letter")
        # RIGHT GUESS
        if guess in remaining_letters:
            character_index = remaining_letters.index(guess)
            letter_board[character_index] = guess
            remaining_letters[character_index] = '$'
        # WRONG GUESS
        else:
            wrong_guesses += 1

        # PRINT LETTER BOARD
        print((' '.join(letter_board)))
        # PRINT HANGMAN
        print('\n'.join(stages[0: wrong_guesses + 1]))

        # ALL GUESSED
        if '__' not in letter_board:
            print('You win! The word was:')
            print(' '.join(letter_board))
            win = True
            break

    # FINAL RESULT
    if not win:
        print('\n'.join(stages[0: wrong_guesses]))
        print('You lose! The words was {}'.format(word))


# __main__
word_list = ["Python", "Java", "computer", "hacker", "painter"]
hangman(word_list)


